package cn.joyfulmedia.common.log;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Splitter;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;
import org.apache.commons.lang3.RandomStringUtils;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.Lists.newArrayList;
import static org.apache.commons.lang3.StringUtils.isNotBlank;

/**
 * 请求App key
 *
 * @author yujun
 * @create 2017-01-22 11:11
 */
public class AppKeyHelper {


    /**
     * 一级来源
     */
    private static Map<String, String> platformMap = new HashMap<String, String>();
    /**
     * 二级来源
     */
    private static Table<String, String, Integer> sourceMap = HashBasedTable.create();

    static {
        platformMap.put("live", "1A");
        platformMap.put("mgtv", "1B");
        platformMap.put("baidu", "1C");
        platformMap.put("live-sdk", "1D");
        platformMap.put("youku", "1E");
    }

    static {
        sourceMap.put("pc", "1a", 1);
        sourceMap.put("h5", "1b", 2);
        sourceMap.put("android", "1c", 3);
        sourceMap.put("ios", "1d", 4);
        sourceMap.put("hardware", "1e", 5);
        sourceMap.put("tv", "1f", 6);
        sourceMap.put("dy", "1g", 7);
        sourceMap.put("ott", "1h", 8);
        sourceMap.put("miwa", "1i", 9);
        sourceMap.put("server", "1j", 10);
    }

    /**
     * 生成app key
     *
     * @param platform 一级业务id,三位数的数字字符串
     * @param source   二级业务id,三位数的数字字符串
     * @return app key
     */
    public static String getAppKey(String platform, String source) throws UnsupportedEncodingException {
        StringBuffer sb = new StringBuffer(RandomStringUtils.random(2, true, true));
        sb.append(platform).append(RandomStringUtils.random(2, true, true));
        sb.append(source).append(RandomStringUtils.random(2, true, true));
        return sb.toString();
    }

    /**
     * 解析App key
     *
     * @param appKey
     * @return
     */
    public static AppKey getSourceType(String appKey) {
        checkArgument(isNotBlank(appKey) && appKey.length() == 10, "appKey不正确");
        Iterable<String> iterable = Splitter.fixedLength(2).split(appKey);
        List<String> info = newArrayList(iterable);
        String source = info.get(3);
        String platform = info.get(1);

        String sourceType;
        switch (source) {
            case "1a":
                sourceType = "pc";
                break;
            case "1b":
                sourceType = "h5";
                break;
            case "1c":
                sourceType = "android";
                break;
            case "1d":
                sourceType = "ios";
                break;
            case "1e":
                sourceType = "hardware";
                break;
            case "1f":
                sourceType = "tv";
                break;
            case "1g":
                sourceType = "dy";
                break;
            case "1h":
                sourceType = "ott";
                break;
            case "1i":
                sourceType = "miwa";
                break;
            case "1j":
                sourceType = "server";
                break;
            default:
                sourceType = "tv";
                break;
        }

        String platformType = "";
        switch (platform) {
            case "1A":
                platformType = "live";
                break;
            case "1B":
                platformType = "mgtv";
                break;
            case "1C":
                platformType = "baidu";
                break;
            case "1D":
                platformType = "live-sdk";
                break;
            case "1E":
                platformType = "youku";
        }

        return new AppKey(platformType, sourceType);
    }

    public static void main(String[] args) throws UnsupportedEncodingException {

        for (Map.Entry<String, String> platform : platformMap.entrySet()) {
            for (Table.Cell<String, String, Integer> source : sourceMap.cellSet()) {
                String appKey = AppKeyHelper.getAppKey(platform.getValue(), source.getColumnKey());
                System.out.println("【平台】" + platform.getKey() + "   【系统】" + source.getRowKey() + "    【appKey】" + appKey + "   " + JSON.toJSONString(AppKeyHelper.getSourceType(appKey)));
            }
        }
    }

    /**
     * APP key实体信息
     */
    public static class AppKey {
        public String platform;
        public String source;

        public AppKey(String platform, String source) {
            this.platform = platform;
            this.source = source;
        }

        public AppKey() {
        }
    }

    static class Base64 {
        /**
         * 加密
         *
         * @param str
         * @return
         */
        public static String encode(String str) {
            byte[] b = null;
            String s = null;
            try {
                b = str.getBytes("utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            if (b != null) {
                s = new BASE64Encoder().encode(b);
            }
            return s;
        }

        /**
         * 解密
         *
         * @param str
         * @return
         */
        public static String decode(String str) {
            String result = null;
            if (str != null) {
                try {
                    result = new String(new BASE64Decoder().decodeBuffer(str), "utf-8");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return result;
        }
    }
}
